//
//  GGFWNet.m
//  上海杨浦
//
//  Created by FrankChen on 13-5-27.
//  Copyright (c) 2013年 FrankChen. All rights reserved.
//

#import "GGFWNet.h"

@implementation GGFWNet

@synthesize linkArray;
@synthesize titleArray;

-(NSString *) getHtml:(NSString *)webUrl{
    
    NSString *retStr =@"";
    
    NSURL *url = [NSURL URLWithString:webUrl];
    
    NSData *data = [NSData dataWithContentsOfURL:url];
    
    retStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    if([retStr isEqual:@""]){
        [self performSelectorOnMainThread:@selector(showAlert) withObject:nil waitUntilDone:NO];
    }
    
    return retStr;
}

-(void)showAlert{
    UIAlertView * alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"网络连接失败" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
    [alert show];
}


-(NSMutableArray *) getData:(NSString *)html{
    NSString *htmlNew=[html stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *tag1=@"<td width='88%' align='left'>";
    NSString *tag2=@"</td>";
    
    NSMutableArray *mutableArray=[[NSMutableArray alloc] init];
    Boolean flag=true;
    //for(int index=0;index<21;index++){
    while (flag) {
        @try {
        
        NSRange range1=[htmlNew rangeOfString:tag1];
        int startIndex1=range1.length+range1.location;
        htmlNew=[htmlNew substringFromIndex:startIndex1];
        
        NSRange range2=[htmlNew rangeOfString:tag2];
        
        int endIndex1=range2.location;
        
        NSString * newInfo=[htmlNew substringToIndex:endIndex1];
        [mutableArray addObject:newInfo];
        
        htmlNew=[htmlNew substringFromIndex:range2.location];
        }
        @catch (NSException *exception) {
            flag=false;
        }
        @finally {
            
        }
    }
    
    return mutableArray;
}


-(void)handleData:(NSMutableArray *)dataArray :(int)flag{
    NSMutableArray *_linkArray=[[NSMutableArray alloc]init];
    NSMutableArray *_titleArray=[[NSMutableArray alloc]init];
    for(NSString *str in dataArray){
        NSString *temp=[[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] substringFromIndex:11];
        //获取url地址
        NSString *tag2=@"<a href='";
        NSRange tag2range=[temp rangeOfString:tag2];
        temp=[temp substringFromIndex:(tag2range.location+tag2range.length)];
        
        NSString *tag1=@"'>";
        int tag1Location=[temp rangeOfString:tag1].location;
        NSString *tempLink1=[temp substringToIndex:(tag1Location)];

        NSString *weburl=@"http://www.shyp.gov.cn";

        NSMutableString *tempLink=[[NSMutableString alloc]initWithString:weburl];
        [tempLink insertString:tempLink1 atIndex:[tempLink length]];
        [_linkArray addObject:tempLink];
        //获取正文标题
        temp=[temp substringFromIndex:(tag1Location+2)];
        //NSString *tag2=@">";
        //int tag2Location=[temp rangeOfString:tag2].location+1;
        //temp=[temp substringFromIndex:tag2Location];

        NSString *tag3=@"</a>";
        int tag3Location=[temp rangeOfString:tag3].location;
        temp=[temp substringToIndex:tag3Location];
        
        //组合正文标题＋时间
        NSMutableString *tempData=[[NSMutableString alloc]init];
        [tempData insertString:temp atIndex:0];
        [_titleArray addObject:tempData];
        
    }
    self.titleArray=_titleArray;
    self.linkArray=_linkArray;
}

@end
